//
//  VideoVC.m
//  KeleAppProject
//
//  Created by 周岑 on 2018/1/19.
//  Copyright © 2018年 Cen Zhou. All rights reserved.
//

#import "VideoVC.h"
#import "KeleNavigationController.h"
#import "RecordViewController.h"
#import "VideoView.h"
#import "SelectTypeView.h"
#import "SelectTagView.h"

@interface VideoVC ()<UIScrollViewDelegate>

// 记录状态   是push  还是 pop  然后根据需求来调整导航栏
@property (nonatomic, assign) BOOL isPush;

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) NSMutableArray *tempTagArray;
@end

@implementation VideoVC

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.isPush = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"tempTag"];
    if (!self.isPush) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
    }
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [self defineSelfNavigation];
    [self loadSubviewsOnVideo];
}

// 自定义导航栏
- (void)defineSelfNavigation{
    self.title = @"发布";
    // 左边
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImageName:@"icon_fanhui" highImageName:@"icon_fanhui" target:self action:@selector(back)];
    // 右边
//    //右边创建2个按钮
//    UIButton *beautyBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//    [beautyBtn setBackgroundImage:[UIImage imageNamed:@"luzhi_icon_meiyan_nor"] forState:UIControlStateNormal];
//    [[beautyBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *beautyBtn) {
//        NSLog(@"哈哈哈");
//
//    }];
//    UIButton *slpashBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//    [slpashBtn setBackgroundImage:[UIImage imageNamed:@"luzhi_icon_guang_guan_nor"] forState:UIControlStateNormal];
//    [[slpashBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *slpashBtn) {
//        NSLog(@"呵呵呵");
//    }];
//    UIButton *cameryBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
//    [cameryBtn setBackgroundImage:[UIImage imageNamed:@"luzhi_icon_fanzhuan"] forState:UIControlStateNormal];
//    [[cameryBtn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(UIButton *cameryBtn) {
//        NSLog(@"大大大");
//    }];
//    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithCustomView:beautyBtn];
//    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:slpashBtn];
//    UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithCustomView:cameryBtn];
//
//    //设置2个按钮之间的间距
//    UIBarButtonItem *itemSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
//    itemSpace.width = 30;
//
//    [self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects: item3,itemSpace,item2,itemSpace,item1,nil]];
}


- (void)back{
    for (UIViewController* vc in self.navigationController.viewControllers) {
        if ([vc isKindOfClass:[RecordViewController class]]) {
            [self.navigationController popToViewController:vc animated:YES];
        }
    }
}


- (void)loadSubviewsOnVideo{
    
    self.scrollView = [[UIScrollView alloc]init];
    self.scrollView.scrollEnabled = YES;
    self.scrollView.backgroundColor = KLColor_C1;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.mas_equalTo(KLScreenH - NaviH);
    }];
    
    UIView *contentView = [[UIView alloc]init];
    contentView.backgroundColor = [UIColor whiteColor];
    self.contentView = contentView;
    [self.scrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollView);
        make.width.mas_equalTo(KLScreenW);
        make.height.mas_equalTo(210*HEIGHT + [VideoView returnHeightWithTagArr:self.tempTagArray]);
    }];
    
    // 头部播放出
    UIView *topPlayView = [[UIView alloc]init];
    topPlayView.backgroundColor = [UIColor blackColor];
    [contentView addSubview:topPlayView];
    [topPlayView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(contentView);
        make.height.mas_equalTo(210*HEIGHT);
    }];
    
    UIButton *changeImageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    changeImageBtn.backgroundColor = KLColor_A1;
    [changeImageBtn setTitle:@"修改封面" forState:UIControlStateNormal];
    [changeImageBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    changeImageBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Semibold" size:14*WIDTH];
    changeImageBtn.layer.masksToBounds = YES;
    changeImageBtn.layer.cornerRadius = 19*HEIGHT;
    [topPlayView addSubview:changeImageBtn];
    [changeImageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(topPlayView);
        make.bottom.equalTo(topPlayView).offset(-20*HEIGHT);
        make.width.mas_equalTo(100*WIDTH);
        make.height.mas_equalTo(39*HEIGHT);
    }];
    
    UIButton *playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [playBtn setBackgroundImage:[UIImage imageNamed:@"luzhi_icon_bofang_xiao"] forState:UIControlStateNormal];
    [topPlayView addSubview:playBtn];
    [playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(topPlayView).offset(-15*WIDTH);
        make.width.height.mas_equalTo(38*WIDTH);
    }];
    
    
    
    
    VideoView *videoView = [[VideoView alloc]init];
    
    videoView.typeSignalDelegate = [RACSubject subject];
    [videoView.typeSignalDelegate subscribeNext:^(id x) {
        NSMutableArray *typeArray = [NSMutableArray arrayWithObjects:@"斤斤计较",@"花费巨大",@"本方法的",@"发空间看",@"发布进度", nil];
        SelectTypeView *typeView = [[SelectTypeView alloc]initWithDataSource:typeArray];
        [self.view addSubview:typeView];
        typeView.typeDelegate = [RACSubject subject];
        [typeView.typeDelegate subscribeNext:^(NSDictionary *x) {
            videoView.selectLb.text = typeArray[[x[@"index"]integerValue]];
        }];
        [typeView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
            make.width.mas_equalTo(KLScreenW);
            make.height.mas_equalTo(KLScreenH);
        }];
    }];
    videoView.tagSignalDelegate = [RACSubject subject];
    [videoView.tagSignalDelegate subscribeNext:^(id x) {
        NSMutableArray *tagArray = [NSMutableArray arrayWithObjects:@"发动机",@"方法",@"大大",@"打发打发",@"发放饭", nil];
        self.tempTagArray = tagArray;
        [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
           
            make.height.mas_equalTo(210*HEIGHT + [VideoView returnHeightWithTagArr:self.tempTagArray]);
        }];
        SelectTagView *tagView = [[SelectTagView alloc]initWithDataSource:tagArray];
        [self.view addSubview:tagView];
        tagView.tagDelegate = [RACSubject subject];
        [tagView.tagDelegate subscribeNext:^(id x) {
            [videoView addTagViewWithData:x];
        }];
        tagView.tagCancelDelegate = [RACSubject subject];
        [tagView.tagCancelDelegate subscribeNext:^(NSArray *x) {
            if (x.count > 0) {
                [videoView addTagViewWithData:x];
            }
        }];
        [tagView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view);
            make.width.mas_equalTo(KLScreenW);
            make.height.mas_equalTo(KLScreenH);
        }];
    }];
    [contentView addSubview:videoView];
    [videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentView).offset(210*HEIGHT);
        make.left.right.bottom.equalTo(contentView);
    }];
}


- (NSMutableArray *)tempTagArray{
    if (!_tempTagArray) {
        _tempTagArray = [NSMutableArray array];
    }
    return _tempTagArray;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
